package com.cinsea.zbc.ext;

import com.alibaba.fastjson.JSON;
import com.cinsea.common.Env;
import com.cinsea.common.ExtAction;
import com.cinsea.common.JdbcDao;
import com.cinsea.util.NumberUtil;
import com.cinsea.util.StringUtil;
import org.eclipse.jdt.internal.compiler.util.FloatUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ZbcAction extends ExtAction{

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        String actionType = StringUtil.null2String(request.getParameter("action"));
        if(actionType.equals("insert")){
            String selectYear = StringUtil.null2String(request.getParameter("selectYear"));
            String selectMonth = StringUtil.null2String(request.getParameter("selectMonth"));
            String orgType = StringUtil.null2String(request.getParameter("orgType"));
            int x = NumberUtil.getIntegerValue(request.getParameter("x"))-1;
            int y = NumberUtil.getIntegerValue(request.getParameter("y"));
            String tabName = "";
            List<Map> mapList = new ArrayList<Map>();
            List<Map> gsList = new ArrayList<Map>();
            for(int r=1; r<=x; r++){
                for(int c=1; c<y; c++){
                    Map map = new HashMap();
                    String userId = StringUtil.null2String(request.getParameter("humanid-"+r));
                    String gzlId = StringUtil.null2String(request.getParameter("orderid-"+c));
                    String confirm = StringUtil.null2String(request.getParameter("confirm-"+r+"-"+c));
                    String reason = StringUtil.null2String(request.getParameter("reason-"+r+"-"+c));
                    map.put("userId",userId);
                    map.put("gzlId",gzlId);
                    map.put("confirm",confirm);
                    map.put("reason",reason);
                    if(confirm.equals("1")){
                        mapList.add(map);
                    }
                }
            }
            if(orgType.equals("technology")){
                tabName="ut_zbc_gongshi6";
            }else{
                tabName = "ut_zbc_gongshi7";
            }
            //删除之前同年月的数据
            String deleteSql = "delete from "+tabName+" where year=? and month=?";
            jdbcDao.update(deleteSql,new Object[]{selectYear,selectMonth});
            //插入数据
            for(Map map : mapList){
                if(map != null){
                    String insertSql = "insert into "+tabName+" (year,month,gzlid,userid,reason,confirm) values("+selectYear+","+selectMonth+",'"+
                                                        map.get("gzlId").toString()+"','"+
                                                        map.get("userId").toString()+"','"+
                                                        map.get("reason").toString()+"',"+
                                                        map.get("confirm").toString()+")";
                        jdbcDao.update(insertSql);
                }
            }
            if(orgType.equals("technology")){
                response.sendRedirect("/zbc/zbc_statistics.jsp?year="+selectYear+"&month="+selectMonth+"&isRedirect=yes");
            }else if(orgType.equals("noTechnology")){
                response.sendRedirect("/zbc/zbc_statistics_fjs.jsp?year="+selectYear+"&month="+selectMonth+"&isRedirect=yes");
            }
        }
        /**
         * 导出
         */
        if(actionType.equals("export")){
            //先保存
            String orgType = StringUtil.null2String(request.getParameter("orgType"));
            int x = NumberUtil.getIntegerValue(request.getParameter("x"))-1;
            int y = NumberUtil.getIntegerValue(request.getParameter("y"));
            int reference = NumberUtil.getIntegerValue(request.getParameter("reference-1"));
            String selectYear = StringUtil.null2String(request.getParameter("selectYear"));
            String selectMonth = StringUtil.null2String(request.getParameter("selectMonth"));
            int gshjRow = NumberUtil.getIntegerValue(request.getParameter("gshjRow"));
            int maxRow = NumberUtil.getIntegerValue(request.getParameter("maxRow"));
            String tabName = "";
            List<Map> mapList = new ArrayList<Map>();
            List<Map> gsList = new ArrayList<Map>();
            for(int r=1; r<=x; r++){
                for(int c=1; c<y; c++){
                    Map map = new HashMap();
                    String userId = StringUtil.null2String(request.getParameter("humanid-"+r));
                    String gzlId = StringUtil.null2String(request.getParameter("orderid-"+c));
                    String confirm = StringUtil.null2String(request.getParameter("confirm-"+r+"-"+c));
                    String reason = StringUtil.null2String(request.getParameter("reason-"+r+"-"+c));
                    map.put("userId",userId);
                    map.put("gzlId",gzlId);
                    map.put("confirm",confirm);
                    map.put("reason",reason);
                    if(confirm.equals("1")){
                        mapList.add(map);
                    }
                }
            }
            if(orgType.equals("technology")){
                tabName="ut_zbc_gongshi6";
            }else{
                tabName = "ut_zbc_gongshi7";
            }
            //删除之前同年月的数据
            String deleteSql = "delete from "+tabName+" where year=? and month=?";
            jdbcDao.update(deleteSql,new Object[]{selectYear,selectMonth});
            //插入数据
            for(Map map : mapList){
                if(map != null){
                    String insertSql = "insert into "+tabName+" (year,month,gzlid,userid,reason,confirm) values("+selectYear+","+selectMonth+",'"+
                            map.get("gzlId").toString()+"','"+
                            map.get("userId").toString()+"','"+
                            map.get("reason").toString()+"',"+
                            map.get("confirm").toString()+")";
                    jdbcDao.update(insertSql);
                }
            }

            String orgtype = "";
            if(orgType.equals("technology")){
                orgtype="1";
            }else{
                orgtype="0";
            }
            for(int k=1; k<=x ; k++){
                Map gsMap = new HashMap();
                String userId = StringUtil.null2String(request.getParameter("humanid-"+k));
                int ckgs = NumberUtil.getIntegerValue(request.getParameter("reference-" + k));
                int valid = NumberUtil.getIntegerValue(request.getParameter("valid-"+k));
                int invalid = NumberUtil.getIntegerValue(request.getParameter("invalid-"+k));
                gsMap.put("orgtype",orgtype);
                gsMap.put("ckgs",ckgs);
                gsMap.put("valid",valid);
                gsMap.put("invalid",invalid);
                gsMap.put("userid",userId);
                gsList.add(gsMap);
            }
            //删除相同组织类型的数据
            String sql = "delete from ut_zbc_gongshi8 where orgtype='"+orgtype+"'";
            jdbcDao.update(sql);
            //再插入数据
            for(Map gs : gsList){
                String updateSql = "insert into ut_zbc_gongshi8 (orgtype,userid,sjckgs,yxgs,wxgs) values('"+
                        gs.get("orgtype").toString()+"','"+
                        gs.get("userid").toString()+"',"+
                        gs.get("ckgs")+","+
                        gs.get("valid")+","+
                        gs.get("invalid")+")";
                jdbcDao.update(updateSql);
            }
            List<Map> ckxxList = new ArrayList<Map>();
            for(int i=1; i<=y; i++){
                for(int a=gshjRow; a<maxRow; a++){
                    Map ckxx = new HashMap();
                    String gzlid = StringUtil.null2String(request.getParameter("orderid-"+i));
                    float gshj = Float.parseFloat(StringUtil.null2String(request.getParameter("gshj-" + a + "-" + i)));
                    a=a+1;
                    float fyhj = Float.parseFloat(StringUtil.null2String(request.getParameter("fyhj-"+a+"-"+i)));
                    a=a+2;
                    String qztime = StringUtil.null2String(request.getParameter("begin-end-"+a+"-"+i));
                    a=a+1;
                    double zys = Double.parseDouble(StringUtil.null2String(request.getParameter("zys-"+a+"-"+i)));
                    a=a+1;
                    double yfryfyys = Double.parseDouble(StringUtil.null2String(request.getParameter("yfryfyys-"+a+"-"+i)));
                    a=a+1;
                    double year_yfryfyys =Double.parseDouble(StringUtil.null2String(request.getParameter("year-yfryfyys-"+a+"-"+i)));
                    a=a+1;
                    double ljzzc =Double.parseDouble(StringUtil.null2String(request.getParameter("ljzzc-"+a+"-"+i)));
                    a=a+1;
                    double ljyfryfyzc =Double.parseDouble(StringUtil.null2String(request.getParameter("ljyfryfyzc-"+a+"-"+i)));
                    a=a+1;
                    double cz1 =Double.parseDouble(StringUtil.null2String(request.getParameter("cz1-"+a+"-"+i)));
                    a=a+1;
                    double cz2 = Double.parseDouble(StringUtil.null2String(request.getParameter("cz2-"+a+"-"+i)));
                    a=a+1;
                    double cz3 = Double.parseDouble(StringUtil.null2String(request.getParameter("cz3-"+a+"-"+i)));
                    ckxx.put("orgtype",orgtype);
                    ckxx.put("gzlid",gzlid);
                    ckxx.put("gshj",gshj);
                    ckxx.put("fyhj",fyhj);
                    ckxx.put("qztime",qztime);
                    ckxx.put("zys",zys);
                    ckxx.put("yfryfyys",yfryfyys);
                    ckxx.put("_yfryfyys",year_yfryfyys);
                    ckxx.put("ljzzc",ljzzc);
                    ckxx.put("ljyfryfyzc",ljyfryfyzc);
                    ckxx.put("cz1",cz1);
                    ckxx.put("cz2",cz2);
                    ckxx.put("cz3",cz3);
                    ckxxList.add(ckxx);
                }
            }

            //删除gongshi9表中同一组织类型的数据
            sql = "delete from ut_zbc_gongshi9 where orgtype='"+orgtype+"'";
            jdbcDao.update(sql);

            //再插入数据
            for(Map info : ckxxList){
                String insertSql = "insert into ut_zbc_gongshi9(orgtype,gzlid,dygs_total,dyfy_total,xmqzsj, xmzys,yfryfyys," +
                        "ndyfryfyys,ljzzc,ljyffyzc,cz1,cz2,cz3) values('"+orgtype+"','"+
                        info.get("gzlid").toString()+"',"+
                        info.get("gshj").toString()+","+
                        info.get("fyhj").toString()+",'"+
                        info.get("qztime").toString()+"',"+
                        info.get("zys").toString()+","+
                        info.get("yfryfyys").toString()+","+
                        info.get("_yfryfyys").toString()+","+
                        info.get("ljzzc").toString()+","+
                        info.get("ljyfryfyzc").toString()+","+
                        info.get("cz1").toString()+","+
                        info.get("cz2").toString()+","+
                        info.get("cz3").toString()+")";
                jdbcDao.update(insertSql);
            }
            if(orgType.equals("technology")){
                response.sendRedirect("/ReportServer?reportlet=/zbc/08_export/tech_gongshi_cost.cpt&pyear="+selectYear+"&pmonth="+selectMonth+"&orgtype=1");
            }else if(orgType.equals("noTechnology")){
                response.sendRedirect("/ReportServer?reportlet=/zbc/08_export/notech_gongshi_cost.cpt&pyear="+selectYear+"&pmonth="+selectMonth+"&orgtype=0");
            }
        }

        /**
         * ajax查询工资率
         */
        if(actionType.equals("allCost")){
            Map gzlMap = new HashMap();
            String year = StringUtil.null2String(request.getParameter("year"));
            String month = StringUtil.null2String(request.getParameter("month"));
            String sql = "select ygbh,dwxsgzl from ut_empmonthrate where year=? and month=?";//员工编号，单位小时工资率
            List gzlList = jdbcDao.queryForList(sql,new Object[]{year,month});

            gzlMap.put("gzllist",gzlList);
//            System.out.println(JSON.toJSONString(gzlMap));
            response.getWriter().print(JSON.toJSONString(gzlMap));
        }
    }
}
